package com.ruoyi.web.controller.account;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BurstingDiscAccount;
import com.ruoyi.system.domain.vo.DeleteVo;
import com.ruoyi.system.service.BurstingDiscAccountService;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 爆破片台账(BurstingDiscAccount)表控制层
 *
 * @author makejava
 * @since 2022-12-14 12:00:12
 */
@RestController
@RequestMapping("bursting/disc/account")
public class BurstingDiscAccountController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private BurstingDiscAccountService burstingDiscAccountService;

    /**
     * 分页查询
     *
     * @param burstingDiscAccount 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public TableDataInfo queryByPage(BurstingDiscAccount burstingDiscAccount) {
        startPage();
        List<BurstingDiscAccount> list = burstingDiscAccountService.queryAll(burstingDiscAccount);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<BurstingDiscAccount> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.burstingDiscAccountService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param burstingDiscAccount 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public ResponseEntity<BurstingDiscAccount> add(@Validated @RequestBody BurstingDiscAccount burstingDiscAccount) {
        return ResponseEntity.ok(this.burstingDiscAccountService.insert(burstingDiscAccount));
    }

    /**
     * 编辑数据
     *
     * @param burstingDiscAccount 实体
     * @return 编辑结果
     */
    @PutMapping("edit")
    public ResponseEntity<BurstingDiscAccount> edit(@Validated @RequestBody BurstingDiscAccount burstingDiscAccount) {
        return ResponseEntity.ok(this.burstingDiscAccountService.update(burstingDiscAccount));
    }

    /**
     * 删除数据
     *
     * @param deleteVo 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    @ApiOperation("删除")
    public ResponseEntity<Boolean> deleteById(@Validated @RequestBody DeleteVo deleteVo) {
        return ResponseEntity.ok(this.burstingDiscAccountService.deleteById(deleteVo.getId()));
    }

    @Log(title = "爆破片台账", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BurstingDiscAccount> util = new ExcelUtil<>(BurstingDiscAccount.class);
        List<BurstingDiscAccount> burstingDiscAccounts = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = burstingDiscAccountService.importExcel(burstingDiscAccounts, updateSupport, operName);
        return success(message);
    }

    @Log(title = "爆破片台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BurstingDiscAccount burstingDiscAccount) {
        List<BurstingDiscAccount> list = burstingDiscAccountService.queryAll(burstingDiscAccount);
        ExcelUtil<BurstingDiscAccount> util = new ExcelUtil<BurstingDiscAccount>(BurstingDiscAccount.class);
        util.exportExcel(response, list, "爆破片台账");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<BurstingDiscAccount> util = new ExcelUtil<>(BurstingDiscAccount.class);
        util.importTemplateExcel(response, "爆破片台账");
    }

}

